home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 February / EnigmA AMIGA RUN 15 (1997)(G.R. Edizioni)(IT)[!][issue 1997-02][PLANET CD V].iso / enigma / earcd / emula / arosdv19.lha / AROS / clib / qsort.c < prev    next >
C/C++ Source or Header  |  1996-10-24  |  4KB  |  152 lines

  1. /*
  2.     (C) 1995-96 AROS - The Amiga Replacement OS
  3.     $Id: qsort.c,v 1.4 1996/10/19 16:56:28 aros Exp $
  4.     $Log: qsort.c,v $
  5.     Revision 1.4  1996/10/19 16:56:28  aros
  6.     Moved all ANSI C function to here and wrote all neccessary header files.
  7.     Now we have a real basis for our own C lib.
  8.     Added the functions malloc(), free() and strcasecmp()
  9.     Made all functions ANSI C compliant
  10.  
  11.     Revision 1.3  1996/09/21 15:47:52  digulla
  12.     Use Amiga types
  13.     Full ANSI prototypes
  14.  
  15.     Revision 1.2  1996/08/01 17:40:45  digulla
  16.     Added standard header for all files
  17.  
  18.     Desc:
  19.     Lang:
  20. */
  21. #include <exec/types.h>
  22. #include <sys/types.h>
  23. #include <stdlib.h>
  24.  
  25. static inline const char *med3 (const char *, const char *, const char *, int (*)());
  26. static inline void     swapfunc (char *, char *, int, int);
  27.  
  28. #define min(a, b)       (a) < (b) ? a : b
  29.  
  30. /*
  31.  * Qsort routine from Bentley & McIlroy's "Engineering a Sort Function".
  32.  */
  33. #define swapcode(TYPE, parmi, parmj, n) {               \
  34.     long i = (n) / sizeof (TYPE);                   \
  35.     register TYPE *pi = (TYPE *) (parmi);           \
  36.     register TYPE *pj = (TYPE *) (parmj);           \
  37.     do {                        \
  38.         register TYPE    t = *pi;        \
  39.         *pi++ = *pj;                \
  40.         *pj++ = t;                \
  41.     } while (--i > 0);                              \
  42. }
  43.  
  44. #define SWAPINIT(a, es) swaptype = ((char *)a - (char *)0) % sizeof(long) || \
  45.     es % sizeof(long) ? 2 : es == sizeof(long)? 0 : 1;
  46.  
  47. static inline void
  48. swapfunc (char *a, char *b, int n, int swaptype)
  49. {
  50.     if(swaptype <= 1)
  51.         swapcode(long, a, b, n)
  52.     else
  53.         swapcode(char, a, b, n)
  54. }
  55.  
  56. #define swap(a, b)                                      \
  57.     if (swaptype == 0) {                            \
  58.         long t = *(long *)(a);                  \
  59.         *(long *)(a) = *(long *)(b);            \
  60.         *(long *)(b) = t;                       \
  61.     } else                        \
  62.         swapfunc(a, b, es, swaptype)
  63.  
  64. #define vecswap(a, b, n)        if ((n) > 0) swapfunc(a, b, n, swaptype)
  65.  
  66. static inline const char *
  67. med3(const char *a, const char *b, const char *c, int (*cmp)(const void *, const void *))
  68. {
  69.     return cmp(a, b) < 0 ?
  70.            (cmp(b, c) < 0 ? b : (cmp(a, c) < 0 ? c : a ))
  71.           :(cmp(b, c) > 0 ? b : (cmp(a, c) < 0 ? a : c ));
  72. }
  73.  
  74. void
  75. qsort(void *a, size_t n, size_t es, int (*cmp)(const void *, const void *))
  76. {
  77.     char *pa, *pb, *pc, *pd, *pl, *pm, *pn;
  78.     int d, r, swaptype, swap_cnt;
  79.  
  80. loop:    SWAPINIT(a, es);
  81.     swap_cnt = 0;
  82.     if (n < 7) {
  83.         for (pm = a + es; pm < (char *) a + n * es; pm += es)
  84.             for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0;
  85.                  pl -= es)
  86.                 swap(pl, pl - es);
  87.         return;
  88.     }
  89.     pm = a + (n / 2) * es;
  90.     if (n > 7) {
  91.         pl = a;
  92.         pn = a + (n - 1) * es;
  93.         if (n > 40) {
  94.             d = (n / 8) * es;
  95.             pl = (char *)med3(pl, pl + d, pl + 2 * d, cmp);
  96.             pm = (char *)med3(pm - d, pm, pm + d, cmp);
  97.             pn = (char *)med3(pn - 2 * d, pn - d, pn, cmp);
  98.         }
  99.         pm = (char *)med3(pl, pm, pn, cmp);
  100.     }
  101.     swap(a, pm);
  102.     pa = pb = a + es;
  103.  
  104.     pc = pd = a + (n - 1) * es;
  105.     for (;;) {
  106.         while (pb <= pc && (r = cmp(pb, a)) <= 0) {
  107.             if (r == 0) {
  108.                 swap_cnt = 1;
  109.                 swap(pa, pb);
  110.                 pa += es;
  111.             }
  112.             pb += es;
  113.         }
  114.         while (pb <= pc && (r = cmp(pc, a)) >= 0) {
  115.             if (r == 0) {
  116.                 swap_cnt = 1;
  117.                 swap(pc, pd);
  118.                 pd -= es;
  119.             }
  120.             pc -= es;
  121.         }
  122.         if (pb > pc)
  123.             break;
  124.         swap(pb, pc);
  125.         swap_cnt = 1;
  126.         pb += es;
  127.         pc -= es;
  128.     }
  129.     if (swap_cnt == 0) {  /* Switch to insertion sort */
  130.         for (pm = a + es; pm < (char *) a + n * es; pm += es)
  131.             for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0;
  132.                  pl -= es)
  133.                 swap(pl, pl - es);
  134.         return;
  135.     }
  136.  
  137.     pn = a + n * es;
  138.     r = min(pa - (char *)a, pb - pa);
  139.     vecswap(a, pb - r, r);
  140.     r = min(pd - pc, pn - pd - es);
  141.     vecswap(pb, pn - r, r);
  142.     if ((r = pb - pa) > es)
  143.         qsort(a, r / es, es, cmp);
  144.     if ((r = pd - pc) > es) {
  145.         /* Iterate rather than recurse to save stack space */
  146.         a = pn - r;
  147.         n = r / es;
  148.         goto loop;
  149.     }
  150. /*        qsort(pn - r, r / es, es, cmp);*/
  151. }
  152.